﻿namespace Vibstudio.NetConfigurator.UI.ViewModels
{
    #region Namespaces

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;

    using Vibstudio.NetConfigurator.Engine.Validation;
    using Vibstudio.NetConfigurator.UI.Common;
    using Vibstudio.NetConfigurator.UI.Model;

    #endregion

    public class ResultConfigurationViewModel : ViewModelBase
    {
        #region Fields

        private ResultConfigurationCollection _result;
        private bool _isEnabled;
        private string _resultMessage;
        private string _rootPath;
        private bool _operationInProgress;

        private RelayCommand _executeValidationUrlCommand;
        private RelayCommand _goToHomeCommand;

        #endregion

        #region Ctor(s)

        public ResultConfigurationViewModel(ResultConfigurationCollection result, string rootPath)
        {
            Result = result;
            IsEnabled = true;
            RootPath = rootPath;
            OperationInProgress = false;
        }

        #endregion

        #region Public Properties

        public ICommand ExecuteValidationUrlCommand
        {
            get
            {
                return _executeValidationUrlCommand ??
                       (_executeValidationUrlCommand =
                        new RelayCommand(param => ExecuteValidation()));
            }
        }

        public ICommand GoToHomeCommand
        {
            get { return _goToHomeCommand ?? (_goToHomeCommand = new RelayCommand(GoToHome)); }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                base.OnPropertyChanged("IsEnabled");
            }
        }

        public ResultConfigurationCollection Result
        {
            get { return _result; }
            set
            {
                _result = value;
                base.OnPropertyChanged("Result");
            }
        }

        public string ResultMessage
        {
            get { return _resultMessage; }
            set
            {
                _resultMessage = value;
                base.OnPropertyChanged("ResultMessage");
            }
        }

        public string RootPath
        {
            get { return _rootPath; }
            set
            {
                _rootPath = value;
                base.OnPropertyChanged("RootPath");
            }
        }

        public bool OperationInProgress
        {
            get { return _operationInProgress; }
            set
            {
                _operationInProgress = value;
                base.OnPropertyChanged("OperationInProgress");

            }
        }

        #endregion

        #region Methods

        private void GoToHome(object o)
        {
            ViewModelLocator.SetChild(ViewRepository.Get.Home());
        }

        private void ExecuteValidation()
        {
            OperationInProgress = true;
            IsEnabled = false;

            var validationWorker = new BackgroundWorker();
            validationWorker.DoWork += ValidationWorkerDoWork;
            validationWorker.RunWorkerCompleted += ValidationWorkerRunWorkerCompleted;
            validationWorker.RunWorkerAsync(this);
        }

        private void ValidationWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = new ResultValidationUrlCollection(new HttpValidator(_rootPath).Validate());
        }

        private void ValidationWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                ViewModelLocator.SetChild(
                    ViewRepository.Get.ResultValidationUrl(
                        e.Result as ResultValidationUrlCollection));
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, e.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                OperationInProgress = false;
                IsEnabled = true;
            }
        }

        #endregion
    }
}